package wordeditor.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Scanner;

public class ConverterROT13 {

	
	public static void convertFile(File filesrc, File filesCible ) {
		String encodedTxt = convertFile(filesrc) ;
		try {
			Writer w = new FileWriter(filesCible) ;
			w.write(encodedTxt);
			w.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String convertFile(File file) {
		String r = "" ;		
		BufferedReader br = null;
		String everything = "" ;
		try {
			br = new BufferedReader(new FileReader(file));
			StringBuilder sb = new StringBuilder();
	        String line = br.readLine();
	        while (line != null) {
	            sb.append(line);
	            sb.append("\n");
	            line = br.readLine();
	        }
	        everything = sb.toString();
	        br.close();		
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		try {
			r = getEncodedText(everything) ;
			System.out.println(r) ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return r ;
	}
	
	
	public static String getEncodedText(String input) {
		String retour = null ;
		if (input != null) {
			retour = "" ;
			String s = input;
	        for (int i = 0; i < s.length(); i++) {
	            char c = s.charAt(i);
	            if       (c >= 'a' && c <= 'm') c += 13;
	            else if  (c >= 'A' && c <= 'M') c += 13;
	            else if  (c >= 'n' && c <= 'z') c -= 13;
	            else if  (c >= 'N' && c <= 'Z') c -= 13;
	            retour += c ;
	        }
		}
        return retour ;
	}
	
	
	
}
